# -*- coding:utf-8 -*-

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

#img = cv.imread('../pic/rice.png', 0)
img = cv.imread('../pic/pic6.png', 0)
# blur = cv.GaussianBlur(img, (3,3), 1.0)
#cv.imshow('blur', blur)

##计算灰度直方图
hist = cv.calcHist(img, [0], None, [256], [0,256])
plt.subplot(121)
plt.imshow(img, 'gray')
plt.xticks([])
plt.yticks([])
plt.title('original')
plt.subplot(122)
plt.hist(img.ravel(), 256, [0,256])
plt.show()
print("plt?")

'''使用局部阈值的大津算法 进行二值化
大津算法有一定的局限性:对于渐变图像已经噪点图像处理结果不理想
'''
black = cv.adaptiveThreshold(img, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 101, 1)
cv.imshow('ostu adaptive', black)
##形态学去燥, 可以去掉一些孤立点
ele = cv.getStructuringElement(cv.MORPH_CROSS, (3,3))
black2 = cv.morphologyEx(black, cv.MORPH_OPEN, ele)
cv.imshow('morpho-open', black2)



###区域生长法
img = cv.imread("../pic/pic6.png")
h,w = img.shape[:2]
print("shape: ",img.shape)
mask = np.zeros([h+2, w+2], np.uint8)
x,water,m,r = cv.floodFill(img, mask, (0,80),(20,100,200))
cv.imshow("waterFlood", water)
cv.waitKey()